//
//  IMB_TimeFormatModel.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-6-29.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "IMB_TimeFormatModel.h"

@implementation IMB_TimeFormatModel

- (id)init {
    if (self = [super init]) {
        // code...
    }
    return self;
}

+ (NSString *)timeFormat:(NSDate *)lastDate {
    @autoreleasepool {
        // 1.获得年月日
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSUInteger unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit |NSMinuteCalendarUnit;
        NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:lastDate];
        NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
        
        // 2.格式化日期
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        if ([cmp1 day] == [cmp2 day] && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"今天HH:mm";
        }else if ([cmp2 day]-[cmp1 day] == 1  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"昨天HH:mm";
        }else if ([cmp2 day]-[cmp1 day] == 2  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"前天HH:mm";
        }else if ([cmp2 day]-[cmp1 day] == 3  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"3天前";
        }else if ([cmp2 day]-[cmp1 day] == 4  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"4天前";
        }else if ([cmp2 day]-[cmp1 day] == 5  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"5天前";
        }else if ([cmp2 day]-[cmp1 day] == 6  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"6天前";
        }else if ([cmp2 day]-[cmp1 day] == 7  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"一周前";
        }else if (([cmp2 day]-[cmp1 day] <= 14 && [cmp2 day] - [cmp1 day] > 7)  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"两周前";
        }else if (([cmp2 day]-[cmp1 day] <= 28 && [cmp2 day] - [cmp1 day] > 14)  && [cmp1 month] == [cmp2 month] && [cmp1 year] == [cmp2 year]) {
            formatter.dateFormat = @"三周前";
        }else {
            formatter.dateFormat = @"MM-dd HH:mm";
        }
        NSString *time = [formatter stringFromDate:lastDate];
        return time;
    }
}

+ (NSString *)clacBabyAge:(NSString *)babyBirthday {
    // 例如:2014-07-16 15:41:13
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate* toDate = [dateFormatter dateFromString:babyBirthday];
    NSDate*  startDate = [ [ NSDate alloc] init ];
    NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
    NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    
    NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0];
    
    NSInteger diffYear = -[cps year];
    NSInteger diffMon = -([cps month]-1);
    
    NSString *result = [NSString stringWithFormat:@"宝贝%ld岁%ld个月",(long)diffYear,(long)diffMon];
    return result;
    
    /*NSInteger diffDay = [cps day];
     NSInteger diffHour = [cps hour];
     NSInteger diffMin = [cps minute];
     NSInteger diffSec = [cps second];
     NSLog(@" From Now to %@, diff: Years: %d  Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d", [toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );*/
}

@end
